package finance;


import java.math.BigDecimal;

import main.ItemviewChart;
import main.Principale;
import mysql.RequeteSql;

import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QPixmap;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QVBoxLayout;
import com.trolltech.qt.gui.QWidget;

public class PrevisionStock extends QWidget
{

	private QHBoxLayout layout;
	private QVBoxLayout boxLayoutLogo, boxLayoutStat;
	private Object  obProduitAAcheter[][], obProduitList[][];
	private Principale parent;
	private QPushButton butAnnuler;
	
	
	void init()
	{
		
		butAnnuler = new QPushButton("Annuler");
		boxLayoutLogo = new QVBoxLayout();
		boxLayoutStat = new QVBoxLayout();
		layout = new QHBoxLayout();
		obProduitAAcheter = RequeteSql.nbrProduitAacheter();
		obProduitList = RequeteSql.produitList();
		butAnnuler.clicked.connect(this, "slotClose()");
		/*this.setStyleSheet("\n"+
				" background-image:url( classpath:api/images/prevstockfond.png);\n"+
				"background-repeat: no-repeat;\n"+
				"background-position: center;\n"+
				"background-color:white;\n"+
				"");*/
	}
	
	
	void placeWidget()
	{
		QGroupBox butBox = new QGroupBox();
		QHBoxLayout butLayout = new QHBoxLayout();
		QHBoxLayout formule1ernivoLayout = new QHBoxLayout();
		butLayout.addWidget(butAnnuler);
		butBox.setLayout(butLayout);
		QLabel labIcone = new QLabel();
		//labIcone.setFixedSize(350, 400);
		labIcone.setPixmap(new QPixmap("classpath:api/images/prevstock.jpg"));
		boxLayoutLogo.addWidget(labIcone);
		//boxLayoutStat.addWidget(formuleLayout());
		//boxLayoutStat.addWidget(stockLayout());
		formule1ernivoLayout.addWidget(formuleLayout());
		formule1ernivoLayout.addWidget(stockLayout());
		boxLayoutStat.addLayout(formule1ernivoLayout);
		boxLayoutStat.addWidget(previsionStockLayout());
		boxLayoutStat.addWidget(butAnnuler);
		//butAnnuler.setFixedSize(100, 50);
		
		layout.addLayout(boxLayoutLogo);
		layout.addLayout(boxLayoutStat);
		//
		//this.setLayout(layout);
		
	}
	
	
	private QGroupBox formuleLayout()
	{
		String tabColor[] = new String[11];
		int prodRestant = 0;
		
		tabColor[0] = "#de0f0f";
		tabColor[1] = "#140fde";
		tabColor[2] =  "#377b25";
		tabColor[3] =  "#c21af9";
		tabColor[4] =  "#00fff6";
		tabColor[5] =  "#a09fa1";
		tabColor[6] =  "#2fea87";
		tabColor[7] =  "#030f09";
		tabColor[8] = "#026430";
		tabColor[9] = "#fcff00";
		tabColor[10] =  "#fd9c1e";
		
		
		QGroupBox groupeForm = new QGroupBox(tr("Stock a acheter"));
		ItemviewChart cam1 = new ItemviewChart(170);
		cam1.setFixedSize(190,190);
		QVBoxLayout lay = new QVBoxLayout();
		QVBoxLayout boxVert = new QVBoxLayout();
		QVBoxLayout laynotice = new QVBoxLayout();
		QLabel labTextDesc = new QLabel("Voici les articles a acheter pour la fin de ce mois");
		//labTextDesc.setFixedWidth(200);
		laynotice.addWidget(labTextDesc);
		
		QHBoxLayout formLayBox = new QHBoxLayout();
		formLayBox.addLayout(lay);
		//QHBoxLayout formLayBox = new QHBoxLayout();
		//QLabel titre = new QLabel("FORMULE");
		
	
		for (int i = 0; i < obProduitAAcheter.length; i++)
		{//setStyleSheet("QLabel { background-color : red; color : blue; }");
			int j = 0;
			prodRestant = 0;
			prodRestant = (Integer)obProduitAAcheter[i][3] - ((BigDecimal)obProduitAAcheter[i][2]).intValue();
			
			QLabel lab = new QLabel();
			String labText = "";
			labText += (String)obProduitList[i][1] + " : ";
			labText += Math.abs(prodRestant);
			if (prodRestant > 0)
			{
				labText += "(EN TROP)";
			}
			else if (prodRestant < 0)
			{
				labText += "(EN DEFICITE)";
			}
			else
			{
				labText += "(RIEN A ACHETER)";
			}
			lab.setText(labText);
			lay.addWidget(lab);
			lab.setStyleSheet("QLabel { color : " + tabColor[i] + "}");
			if (prodRestant < 0)
			{
				j++;
			cam1.rempliCamember((String)obProduitList[i][1], String.valueOf(Math.abs(prodRestant)), tabColor[i], j);
			}
		}
		//formLayBox.addWidget(titre);
		//formLayBox.addLayout(lay);
		
		
		
		formLayBox.addWidget(cam1);
		
		boxVert.addLayout(laynotice);
		boxVert.addLayout(formLayBox);
		groupeForm.setLayout(boxVert);
		groupeForm.setFixedHeight(250);
		
		return groupeForm;
		
	}
	
	
	public void slotClose()
	{
		RequeteSql.TAP_closeConnection();
	    parent.ouvertureFenetreStatisciq();
	    close();
	}
	
	
	private QGroupBox stockLayout()
	{
		String tabColor[] = new String[11];
		
		tabColor[0] = "#de0f0f";
		tabColor[1] = "#140fde";
		tabColor[2] =  "#377b25";
		tabColor[3] =  "#c21af9";
		tabColor[4] =  "#00fff6";
		tabColor[5] =  "#a09fa1";
		tabColor[6] =  "#2fea87";
		tabColor[7] =  "#030f09";
		tabColor[8] = "#026430";
		tabColor[9] = "#fcff00";
		tabColor[10] =  "#fd9c1e";
		
		
		
		QGroupBox groupeForm = new QGroupBox(tr("STOCK"));
		ItemviewChart cam = new ItemviewChart(170);
		cam.setFixedSize(190,190);
		QVBoxLayout lay = new QVBoxLayout();
		QVBoxLayout boxVert = new QVBoxLayout();
		QVBoxLayout laynotice = new QVBoxLayout();
		laynotice.addWidget(new QLabel("votre stock contient actuelement"));
		QHBoxLayout formLayBox = new QHBoxLayout();
		formLayBox.addLayout(lay);
		
		
		
		//QLabel titre = new QLabel("FORMULE");
		
	
		for (int i = 0; i < obProduitList.length; i++)
		{
			QLabel lab = new QLabel();
			lab.setText((String)obProduitList[i][1] + " : " + (Integer)obProduitList[i][3] + " ");
			lay.addWidget(lab);
			lab.setStyleSheet("QLabel { color : " + tabColor[i] + "}");

			cam.rempliCamember((String)obProduitList[i][1], String.valueOf((Integer)obProduitList[i][3]), tabColor[i], i);
		}
		//formLayBox.addWidget(titre);
		formLayBox.addLayout(lay);
		
		
		
		formLayBox.addWidget(cam);
		boxVert.addLayout(laynotice);
		boxVert.addLayout(formLayBox);
		groupeForm.setLayout(boxVert);
		
		groupeForm.setFixedHeight(250);
		return groupeForm;
		
	}
	
	private QGroupBox previsionStockLayout()
	{
		String tabColor[] = new String[11];
		
		tabColor[0] = "#de0f0f";
		tabColor[1] = "#140fde";
		tabColor[2] =  "#377b25";
		tabColor[3] =  "#c21af9";
		tabColor[4] =  "#00fff6";
		tabColor[5] =  "#a09fa1";
		tabColor[6] =  "#2fea87";
		tabColor[7] =  "#030f09";
		tabColor[8] = "#026430";
		tabColor[9] = "#fcff00";
		tabColor[10] =  "#fd9c1e";
		
		
		
		QGroupBox groupeForm = new QGroupBox(tr("Prevision stock"));
		ItemviewChart cam = new ItemviewChart(170);
		cam.setFixedSize(190,190);
		QVBoxLayout lay = new QVBoxLayout();
		QVBoxLayout boxVert = new QVBoxLayout();
		QVBoxLayout laynotice = new QVBoxLayout();
		laynotice.addWidget(new QLabel("Voici les quantites minimales de produits qui devront etre en stock"));
		QHBoxLayout formLayBox = new QHBoxLayout();
		formLayBox.addLayout(lay);
		//QLabel titre = new QLabel("FORMULE");
		
	
		for (int i = 0; i < obProduitAAcheter.length; i++)
		{
			QLabel lab = new QLabel();
			lab.setText((String)obProduitAAcheter[i][1] + " : " + String.valueOf(((BigDecimal)obProduitAAcheter[i][2]).intValue() + " "));
			lay.addWidget(lab);
			lab.setStyleSheet("QLabel { color : " + tabColor[i] + "}");

			cam.rempliCamember((String)obProduitAAcheter[i][1], String.valueOf(((BigDecimal)obProduitAAcheter[i][2]).intValue() + " "), tabColor[i], i);
		}
		//formLayBox.addWidget(titre);
		//formLayBox.addLayout(laynotice);
		
		
		
		
		formLayBox.addWidget(cam);
		
		boxVert.addLayout(laynotice);
		boxVert.addLayout(formLayBox);
		groupeForm.setLayout(boxVert);
		groupeForm.setFixedHeight(250);
		return groupeForm;
		
	}
	
	public PrevisionStock(Principale parent)
	{
		super(parent);
		this.parent = parent;
		RequeteSql.TAP_InitialiserConnectionJDBC();
		init();
		placeWidget();
		
		
		//cam1.setFixedSize(150,150);
		this.setLayout(layout);
		//layout.addWidget(cam1);
		//layout.addWidget(cam2);
		//layout.addWidget(cam3);
		
		
		
	}
	/*
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		QApplication.initialize(args);

		PrevisionStock mainW = new PrevisionStock();
        mainW.show();

        QApplication.exec();
	}
*/
}
